package org.dawn.deveco.plugin.resourceeditor.model;

import org.dawn.deveco.plugin.resourceeditor.model.interfaces.ResourceContent;
import org.dawn.deveco.plugin.resourceeditor.model.item.BaseItem;

import java.util.*;

public abstract class BaseResourceContent implements ResourceContent {

    /**
     * 资源类别：string, color, dimension, media
     */
    protected String category;

    /**
     * 列数据类型
     */
    protected ResourceContent.ColumnConfig[] columnConfigs = new ResourceContent.ColumnConfig[0];

    protected boolean[] columnEditable = null;

    protected boolean[] columnSelectable = null;

    /**
     * 从资源文件加载的初始数据
     */
    protected final Map<String, List<? extends BaseItem>> itemListByGroup = new java.util.TreeMap<>();

    /**
     * DefaultTableModel 的表格数据
     */
    protected final Map<String, Vector<Vector>> tableDataByGroup = new java.util.TreeMap<>();

    /**
     * 保留分组在数据文件中的顺序
     */
    protected final List<String> groupList = new ArrayList<>();

    /**
     * 数据是否被成功加载
     */
    protected LoadingState loadingState = LoadingState.SUCCESS_LOADED;

    /**
     * 表格数据是否被修改
     */
    private boolean changed = false;

    public void addGroupData(String groupName, List<? extends BaseItem> itemList) {
        this.groupList.add(groupName);
        this.itemListByGroup.put(groupName, itemList);
    }

    public void syncGroupData(String groupName, Vector<Vector> dataVector) {
        this.tableDataByGroup.put(groupName, dataVector);
    }

    public List<String> getGroupList() {
        return this.groupList;
    }

    public List<? extends BaseItem> getGroupData(String group) {
        return this.itemListByGroup.get(group);
    }

    public boolean save() {
        return false;
    }

    public void setColumns(ResourceContent.ColumnConfig ...columnConfigs) {
        this.columnConfigs = columnConfigs;
    }

    public ResourceContent.ColumnConfig[] getColumns() {
        return this.columnConfigs;
    }

    // region getter/setter
    public String getCategory() {
        return category;
    }

    public void setCategory(String category) {
        this.category = category;
    }

    public Class[] getColumnClasses() {
        return Arrays.stream(columnConfigs).map(e -> e.columnClass).toArray(arr -> new Class[columnConfigs.length]);
    }

    public boolean[] getColumnVisible() {
        boolean[] result = new boolean[columnConfigs.length];
        for (int i = 0; i < columnConfigs.length; i++) {
            result[i] = columnConfigs[i].visible;
        }
        return result;
    }

    public boolean[] getColumnSelectable() {
        boolean[] result = new boolean[columnConfigs.length];
        for (int i = 0; i < columnConfigs.length; i++) {
            result[i] = columnConfigs[i].selectable;
        }
        return result;
    }

    public boolean[] getColumnEditable() {
        if (this.columnEditable == null) {
            this.columnEditable = new boolean[columnConfigs.length];
            for (int i = 0; i < columnConfigs.length; i++) {
                this.columnEditable[i] = columnConfigs[i].editable;
            }
        }
        return this.columnEditable;
    }

    public Vector<String> getHeader() {
        Vector<String> result = new Vector<>(columnConfigs.length);
        for (int i = 0; i < columnConfigs.length; i++) {
            result.add(columnConfigs[i].columnName);
        }
        return result;
    }

    public boolean isChanged() {
        return changed;
    }

    public void setChanged(boolean changed) {
        this.changed = changed;
    }

    @Override
    public LoadingState getLoadingState() {
        return this.loadingState;
    }

    @Override
    public void setLoadingState(LoadingState loadingState) {
        this.loadingState = loadingState;
    }

    // endregion
}
